package errors

// 错误码定义规则：
// 0: 成功
// 1000-1999: 通用系统错误
// 2000-2999: 参数错误
// 3000-3999: 业务逻辑错误（认证、用户等）
// 4000-4999: 业务逻辑错误（抖音相关）
// 5000-5999: 其他业务模块错误

const (
	// ==================== 成功 ====================
	CodeSuccess = 200

	// ==================== 通用错误 (500-1000) ====================
	CodeSystemError = 500 // 系统错误

	// ==================== 参数错误 (2000-2999) ====================
	CodeParamsInvalid = 2000 // 请求参数无效
	CodePhoneFormat   = 2001 // 手机号格式不正确
	CodeTypeInvalid   = 2002 // 验证码类型不正确
	CodeInvalid       = 2003 // 验证码错误
	CodeExpired       = 2004 // 验证码不存在或已过期

	// ==================== 认证相关错误 (3000-3099) ====================
	CodeSendTooFrequent   = 3001 // 发送过于频繁
	CodeDailyLimitExceed  = 3002 // 今日发送次数已达上限
	CodeSMSSendFailed     = 3003 // 短信发送失败
	CodeUserNotFound      = 3004 // 用户不存在
	CodeUserDisabled      = 3005 // 账户已被禁用
	CodeLoginLocked       = 3006 // 账户已被锁定
	CodeTokenInvalid      = 3007 // Token无效
	CodeTokenExpired      = 3008 // Token已过期
	CodeTokenRevoked      = 3009 // Token已被撤销
	CodeRefreshTokenUsed  = 3010 // Refresh Token已使用
	CodeCreateUserFailed  = 3011 // 创建用户失败
	CodeUpdateUserFailed  = 3012 // 更新用户失败
	CodeSaveSMSCodeFailed = 3013 // 保存验证码失败
	CodeUserAlreadyExists = 3014 // 用户已存在

	// ==================== 抖音业务错误 (4000-4999) ====================
	CodeDouyinNoLogin           = 4001 // 未登入
	CodeDouyinParamsInvalid     = 4002 // 参数错误
	CodeDouyinGetConnFailed     = 4010 // 获取连接器失败
	CodeDouyinGetRouterFailed   = 4013 // 获取路由失败
	CodeDouyinGetCmdFailed      = 4014 // 获取下载链接失败
	CodeDouyinGetShellFailed    = 4015 // 获取下载shell失败
	CodeDouyinGetUriFailed      = 4016 // 获取下载链接失败
	CodeDouyinGetEndpointFailed = 4017 // 获取连接器失败
	CodeDouyinIpLimitExceeded   = 4018 // 超出IP限制

	// ==================== 其他业务模块错误 (5000-5999) ====================
	// 预留给其他业务模块使用
)

// ==================== 预定义的通用错误 ====================
var (
	ErrSuccess = NewServiceError(CodeSuccess, "success")

	// 通用错误
	ErrSystem = NewServiceError(CodeSystemError, "system error")

	// 参数错误
	ErrParamsInvalid = NewServiceError(CodeParamsInvalid, "invalid parameters")
	ErrPhoneFormat   = NewServiceError(CodePhoneFormat, "invalid phone format")
	ErrCodeType      = NewServiceError(CodeTypeInvalid, "invalid code type")
	ErrCodeWrong     = NewServiceError(CodeInvalid, "invalid verification code")
	ErrCodeNotFound  = NewServiceError(CodeExpired, "verification code not found or expired")

	// 认证相关错误
	ErrSendTooFrequent   = NewServiceError(CodeSendTooFrequent, "send too frequent")
	ErrDailyLimitExceed  = NewServiceError(CodeDailyLimitExceed, "daily limit exceeded")
	ErrSMSSendFailed     = NewServiceError(CodeSMSSendFailed, "sms send failed")
	ErrUserNotFound      = NewServiceError(CodeUserNotFound, "user not found")
	ErrUserDisabled      = NewServiceError(CodeUserDisabled, "user disabled")
	ErrLoginLocked       = NewServiceError(CodeLoginLocked, "login locked")
	ErrTokenInvalid      = NewServiceError(CodeTokenInvalid, "token invalid")
	ErrTokenExpired      = NewServiceError(CodeTokenExpired, "token expired")
	ErrTokenRevoked      = NewServiceError(CodeTokenRevoked, "token revoked")
	ErrCreateUserFailed  = NewServiceError(CodeCreateUserFailed, "create user failed")
	ErrUpdateUserFailed  = NewServiceError(CodeUpdateUserFailed, "update user failed")
	ErrSaveSMSCode       = NewServiceError(CodeSaveSMSCodeFailed, "save sms code failed")
	ErrUserAlreadyExists = NewServiceError(CodeUserAlreadyExists, "user already exists")

	// 抖音业务错误
	ErrDouyinNoLogin           = NewServiceError(CodeDouyinNoLogin, "douyin not logged in")
	ErrDouyinParamsInvalid     = NewServiceError(CodeDouyinParamsInvalid, "douyin invalid parameters")
	ErrDouyinGetConnFailed     = NewServiceError(CodeDouyinGetConnFailed, "douyin get connector failed")
	ErrDouyinGetRouterFailed   = NewServiceError(CodeDouyinGetRouterFailed, "douyin get router failed")
	ErrDouyinGetCmdFailed      = NewServiceError(CodeDouyinGetCmdFailed, "douyin get download cmd failed")
	ErrDouyinGetShellFailed    = NewServiceError(CodeDouyinGetShellFailed, "douyin get download shell failed")
	ErrDouyinGetUriFailed      = NewServiceError(CodeDouyinGetUriFailed, "douyin get download uri failed")
	ErrDouyinGetEndpointFailed = NewServiceError(CodeDouyinGetEndpointFailed, "douyin get endpoint failed")
	ErrDouyinIpLimitExceeded   = NewServiceError(CodeDouyinIpLimitExceeded, "douyin ip limit exceeded")
)

// NewLoginLockedError 创建带锁定信息的登录锁定错误
func NewLoginLockedError(lockExpTime interface{}, waitSeconds int) *ServiceError {
	return &ServiceError{
		Code:    CodeLoginLocked,
		Message: "login locked",
		Data: map[string]interface{}{
			"lock_exp_time": lockExpTime,
			"wait_seconds":  waitSeconds,
		},
	}
}
